/**
 * 
 */
package com.gordon.tips.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import com.gordon.tips.domain.Reply;
import com.gordon.tips.domain.Topic;
import com.gordon.tips.domain.User;
import com.gordon.tips.log.Logger;
import com.gordon.tips.log.LoggerImpl;
import com.gordon.tips.service.ReplyService;
import com.gordon.tips.service.TopicService;
import com.gordon.tips.util.Constants;

/**
 *****************************************************************************
 ** Module : Reply controler
 ** 
 ** Date:  Aug 18th,2011
 ** 
 ** Author: Gordon
 ** 
 ** Email: 462991284@qq.com
 ** 
 ** (C) Copyright 2011 Gordon Office
 *****************************************************************************
 */

public class ReplyAction extends BaseAction 
{
	private Reply reply;
	private Topic topic;
	
	private int topicId;
	private int authorId;
	private int replyId;
	private int pageIndex;
	
	private ReplyService replyService;
	private TopicService topicService;
	
	private static Logger logger = new LoggerImpl(ReplyAction.class);
	
	/**
	 * Add a new reply
	 * @return
	 * @Author: Gordon
	 */
	public String addNewReply()
	{					
		try {
			String content= java.net.URLDecoder.decode(reply.getContent(),"UTF-8");
			reply.setContent(content);
			logger.debug("reply's content:"+reply.getContent());
		} catch (UnsupportedEncodingException e1) { 
			e1.printStackTrace();
		} 		
		
		reply.setCreateTime(new Date());
		reply.setFloor(topicService.getTopicById(topicId).getReply()+2);
		replyId=replyService.addNewReply(reply, topicId, authorId);
		
		//add score
		User user=(User) session.get("user_session");
		logger.debug("before posting reply,score="+user.getScore());
		user.setScore(user.getScore()+Constants.SCORE_ADD_NEWREPLY);
		user.setPost(user.getPost()+1);
		logger.debug("after post reply,score="+user.getScore());
		session.put("user_session", user);
		
		//warp to json object
		StringBuffer sb=new StringBuffer();
		sb.append("{");
		sb.append("\"authorName\":"+"\""+user.getName()+"\",");
		sb.append("\"authorId\":"+"\""+authorId+"\",");
		sb.append("\"avatar\":"+"\""+"avatar/default.jpg"+"\",");
		sb.append("\"content\":"+"\""+reply.getContent()+"\",");
		sb.append("\"post\":"+"\""+user.getPost()+"\",");
		sb.append("\"score\":"+"\""+user.getScore()+"\",");
		sb.append("\"createTime\":"+"\""+reply.getCreateTime()+"\",");
		sb.append("\"newReplyId\":"+"\""+replyId+"\",");
		sb.append("\"floor\":"+"\""+reply.getFloor()+"\"}");
		
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();			
			out.print(sb.toString());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			out.close();
		}
		return null;
	}
	
	/**
	 * enter editing a reply page
	 * @return
	 * @Author: Gordon
	 */
	public String editReply()
	{
		//Check if there being user in session
		User user=(User) session.get("user_session");
		logger.debug("user="+user);		
		if(user==null)return "ilegalAccess";
		
		//Get the reply content
		reply=replyService.getReplyById(replyId);
		reply.setTopic(new Topic());
		reply.getTopic().setTitle(topic.getTitle());
		reply.getTopic().setId(topic.getId());
		logger.debug("reply="+reply);
		logger.debug("reply's author's id="+reply.getAuthor().getId());
		logger.debug("reply's topic's title="+reply.getTopic().getTitle());
		
		//Check if the reply is posted by the user
		if(reply.getAuthor().getId()!=user.getId())return "ilegalAccess";
		
		//Check if the reply is of 1st floor or not.Difference of editing reply and topic is there being editing the title.
		if(reply.getFloor()==1)return "editTopic";
		return "editReply";
	}
	
	public String updateReply()
	{
		//Check if there being user in session
		User user=(User) session.get("user_session");
		logger.debug("user="+user);		
		if(user==null)return "ilegalAccess";
		
		//Validate the reply's author
		Reply reply2=replyService.getReplyById(reply.getId());
		if(reply2.getAuthor().getId()!=user.getId())return  "ilegalAccess";
		
		PrintWriter out = null;
		replyService.editReply(reply.getId(), reply.getContent());
		
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			out.println("OK");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			out.close();
		}
		return null;			
	}
	

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	public int getTopicId() {
		return topicId;
	}

	public void setTopicId(int topicId) {
		this.topicId = topicId;
	}

	public int getAuthorId() {
		return authorId;
	}

	public void setAuthorId(int authorId) {
		this.authorId = authorId;
	}

	public ReplyService getReplyService() {
		return replyService;
	}

	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}

	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	public int getReplyId() {
		return replyId;
	}

	public void setReplyId(int replyId) {
		this.replyId = replyId;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}


	
	
	
	

}
